package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;
    @Override
    public void save(ExtCproduct ExtCproduct) {
        //获取总钱数
        double money=0.0;
        if(ExtCproduct.getPrice()!=null&&ExtCproduct.getCnumber()!=null) {
            money = ExtCproduct.getCnumber() * ExtCproduct.getPrice();
        }
        ExtCproduct.setAmount(money);
        Contract contract = contractDao.selectByPrimaryKey(ExtCproduct.getContractId());
        contract.setExtNum(contract.getExtNum()+1);
        contract.setTotalAmount(contract.getTotalAmount()+money);
        contractDao.updateByPrimaryKeySelective(contract);
        extCproductDao.insertSelective(ExtCproduct);
    }

    @Override
    public void update(ExtCproduct ExtCproduct) {
        double newMoney=0.0;
        if(ExtCproduct.getCnumber()!=null&&ExtCproduct.getPrice()!=null) {
            newMoney=ExtCproduct.getCnumber()*ExtCproduct.getPrice();
        }
        //修改之前的钱
        double oldMoney = extCproductDao.selectByPrimaryKey(ExtCproduct.getId()).getAmount();
        //合同原来的钱
        Double totalAmount = contractDao.selectByPrimaryKey(ExtCproduct.getContractId()).getTotalAmount();
        Contract contract = contractDao.selectByPrimaryKey(ExtCproduct.getContractId());
        ExtCproduct.setAmount(newMoney);
        contract.setTotalAmount(totalAmount-oldMoney+newMoney);
        extCproductDao.updateByPrimaryKeySelective(ExtCproduct);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        //获取删除之前的钱
        Double amount = extCproductDao.selectByPrimaryKey(id).getAmount();
        Contract contract = contractDao.selectByPrimaryKey(extCproductDao.selectByPrimaryKey(id).getContractId());
        contract.setTotalAmount(contract.getTotalAmount()-amount);
        contract.setExtNum(contract.getExtNum()-1);
        extCproductDao.deleteByPrimaryKey(id);
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ExtCproduct findById(String id) {
        return extCproductDao.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExtCproductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<ExtCproduct> list = extCproductDao.selectByExample(example);
        return new PageInfo(list,5);
    }
}
